<?php

namespace StudyBuddy\Modules\Tumblr;

use \StudyBuddy\StudyBuddyObject;
use \StudyBuddy\Interfaces\Post;
use \StudyBuddy\Interfaces\Question;
use \StudyBuddy\Interfaces\Answer;
use \StudyBuddy\Registry;

/**
 * Adapter class that takes in
 * Question or Answer
 * and returns object TumblrContent which can
 * then be used to post to TumblrApi
 * That object TumblrContent is usually a TumblrPost object
 * but can also be TumblrLink in we decide to just post
 * a Link to Tumblr instead of a full Blog post
 * Enter description here ...
 *
 */
class TumblrPostAdapter extends StudyBuddyObject {

    /**
     * TumblrPost object
     *
     * @var object of type TumblrPost
     */
    protected $oTumblrPost;

    public function __construct(Registry $o) {
        $this->oRegistry = $o;
    }

    /**
     * Main method to get the TumblrPost object
     * from the Question or Answer input object
     *
     * @param Post $o object of type Post which
     * could be Question OR Answer
     *
     * @return object of type TumblrPost
     */
    public function get(Post $o) {
        d('cp');
        if ($o instanceof Question) {
            $this->makeQuestionPost($o);
        } elseif ($o instanceof Answer) {
            $this->makeAnswerPost($o);
        }
        d('cp');

        return $this->getTumblrPost();
    }

    /**
     * Getter for $this->oTumblrPost
     * @return object of type TumblrPost
     */
    public function getTumblrPost() {
        return $this->oTumblrPost;
    }

    /**
     *
     * Setup values in $this->oTumblrPost using
     * values of Question
     *
     * @param Question $o
     */
    protected function makeQuestionPost(Question $o) {
        d('cp');
        $this->oTumblrPost = new TumblrPost();

        /**
         * @todo Translate strings
         *
         * @var string
         */
        $qUrl = $o->getUrl();
        $tpl1 = '<p><a href="%s"><strong>My question</strong></a> on %s</p>';
        $tpl2 = '<p><a href="%s">Click here</a> to post your reply</p><br>';
        $body = sprintf($tpl1, $qUrl, $this->oRegistry->Ini->SITE_NAME);
        $body .= $o->getBody();
        $body .= sprintf($tpl2, $qUrl);

        $this->oTumblrPost->setBody($body)->setTitle($o->getTitle());

        $tags = $o['a_tags'];
        d('$tags: ' . print_r($tags, 1));
        if (!empty($tags)) {
            $this->oTumblrPost->setTags($tags);
        }

        d('cp');
        $this->oTumblrPost->setSlug($o->getSeoUrl());

        return $this;
    }

    /**
     *
     * Setup values in $this->oTumblrPost using
     * values of Answer
     *
     * @param Answer $o
     */
    protected function makeAnswerPost(Answer $o) {
        d('cp');
        $this->oTumblrPost = new TumblrPost();
        $qlink = $this->oRegistry->Ini->SITE_URL . '/q' . $o->getQuestionId() . '/';

        /**
         * @todo Translate string
         *
         * @var string
         */
        $tpl = '<p>This is my answer to a <a href="%s"><strong>question</strong></a> on %s</p><br>';
        $body = sprintf($tpl, $qlink, $this->oRegistry->Ini->SITE_NAME);

        $body .= $o->getBody();
        d('body: ' . $body);

        $title = 'My answer to "' . $o->getTitle() . '"';

        $this->oTumblrPost->setBody($body)->setTitle($title);

        return $this;
    }

}
